<?php
Emos_Config::set(array(
    'developerMode' => true, // Automatic DB scruction update
    'logSQL'        => true 
));


//=============================================================================
// Group+User tree editor
//============================================================================
$tree = new Emos_Tree('example1');

// Group level
$groupLevel = new Emos_TreeLevel('account_group', 'group_id');
$groupLevel->form->setField('name', 'Name', new Emos_Field_Text();

// User level
$userLevel = new Emos_TreeLevel('account_user', 'user_id');
$userLevel->form->setField('login',    'Login',    new Emos_Field_Text();
$userLevel->form->setField('password', 'Password', new Emos_Field_Text();

// Relations between levels
$tree->addEntity($group_level);
$groupLevel->addEntity($userLevel, 'group_id');

// Events processing, recoreds update
$tree->action();

// ... here we can add some extra manipulations with tree like changing fields etc ...

// Sending data back to browser
$tree->ajax();
 
 
 
//=============================================================================
// Recursive catalog with items
//=============================================================================
$tree = new Emos_Tree('example2');

$catalogLevel = new Emos_TreeLevel('catalog_folder', 'id');
$catalogLevel->form->setField('title', 'Title', new Emos_Field_Text());

$itemLevel = new Emos_TreeLevel('catalog_item', 'id');
$itemLevel->form->setField('title',       'Title',       new Emos_Field_Text());
$itemLevel->form->setField('description', 'Description', new Emos_Field_Textarea());

$tree->addEntity($catalogLevel, 'catalog_id');
$catalogLevel->addEntity($catalogLevel, 'catalog_id'); // Reccursion
$catalogLevel->addEntity($itemLevel, 'catalog_id'); 
$tree->ajax();



//=============================================================================
// Recursive catalog with items
//=============================================================================
$tree = new Emos_Tree('catalog');

$catalogLevel = new Emos_TreeLevel('catalog_folder', 'id');
$catalogLevel->form->setField('title', 'Title', new Emos_Field_Text();

$itemLevel = new Emos_TreeLevel('catalog_item', 'id');
$itemLevel->form->setField('title',       'Title',       new Emos_Field_Text();
$itemLevel->form->setField('description', 'Description', new Emos_Field_Textarea();

$tree->addEntity($catalogLevel, 'catalog_id');
$catalogLevel->addEntity($catalogLevel, 'catalog_id'); // Reccursion!
$catalogLevel->addEntity($itemLevel, 'catalog_id'); 
$tree->ajax();




//=============================================================================
// Settings
//=============================================================================
$set = new Emos_Settings('settings');

$set->form->setField('config/text1', 'Text 1', new Emos_Field_Text();
$set->form->setField('config/text2', 'Text 2', new Emos_Field_Textarea(array('cols'=>20, 'rows'=>5, 'style'=>'color:red; background-color:yellow'));

$set->ajax();


//=============================================================================
// Form with few tabs
//=============================================================================
$tree = new Emos_Tree('with_few_tabs');

$lev = new Emos_TreeLevel('table_1', 'id');
$lev->form->setField('field_1', 'Single field 1', new Emos_Field_Text();
$lev->form->setField('field_2', 'Single field 2', new Emos_Field_Text();

$tab1 = new Emos_FormTab('Tab 1');
$tab1->setField('field_a', 'Field A', new Emos_Field_Text());
$tab1->setField('field_b', 'Field B', new Emos_Field_Text());
$tab1->setField('field_c', 'Field C', new Emos_Field_Text());

$tab2 = new Emos_FormTab('Tab 2');
$tab2->setField('field_d', 'Field D', new Emos_Field_Text());
$tab2->setField('field_e', 'Field E', new Emos_Field_Text());

$lev->form->setElement(array($tab1, $tab2));
$lev->form->setField('field_3', 'Single field 3', new Emos_Field_Text();

$tree->addEntity($lev);
$tree->ajax();



//=============================================================================
// Tree as a Table + filter + custom SQL
//============================================================================
$tree = new Emos_Tree('list');
$tree->setOptions(array(
    'width'             =>'100%',
    'height'            =>'500px',
    'listWidth'         =>'40%',
    'editorWidth'       =>'60%',
    'editorOverList'    =>false
));

$lev = new Emos_TreeLevel('person', 'id');

// We can use custom SQL for fetching item list.
// Will be used as a subquery instead of given table
$lev->setItemSourceSql("
    SELECT p.*, COUNT(c.id) AS cars_amount
    FROM person AS p
    LEFT JOIN car AS c ON c.person_id=p.id
    WHERE c.brand <> 'VAZ'
");

// Columns for the list. Will looks as a table when we have more than one column.
$lev->list->setCol('first_name', 'Name', new Emos_Field_Text(array('width'=>'100', 'align'=>'left')));
$lev->list->setCol('age',        'Age',  new Emos_Field_Text( array('width'=>'20',  'align'=>'right')));

$lev->setIPP(20); // Items Per Page value for pager

// Fields for the level filter
$lev->filter->setField('first_name', 'Name', new Emos_FilterField_Text(array('condition'=>'contains')));
$lev->filter->setField('age',        'Age',  new Emos_FilterField_Number(array('min'=>true, 'max'=>true)));

// Form fields
$lev->form->setField('first_name',  'First name',  new Emos_Field_Text());
$lev->form->setField('second_name', 'Second name', new Emos_Field_Text());
$lev->form->setField('age',         'Age',         new Emos_Field_Number());

$tree->ajax();



